//
//  NSUtilLib.m
//  HandPay
//
//  Created by xy on 10-4-29.
//  Copyright 2010 HANDPAY. All rights reserved.
//

#import "NSUtilLib.h"
#import "HPClientEngine.h"
#import "Utility.h"
#import "Directory.h"
#import "SecureManager.h"
//#import "Item.h"
#include "luaCommonlib.h"

#include "DefineUtil.h"

static NSUtilLib * timerutil=nil;

void ClearKey(){
    HPClientEngine * ce=[HPClientEngine getHPClientEngine];
    [ce.securmanager ReSetSecure:nil];
}

void UtilThreadExec(HPData * fuc,HPData ** args,int argnum){
	/*ClientEngine * ce=[ClientEngine getClientEngine];
	HPFunctionData * cbtdata=[[HPFunctionData alloc] initwithFunc:fuc];
	cbtdata.paramsNum=argnum;
	cbtdata.paramsdatas=args;
	//[ce threadExec:cbtdata];
	NSThread * thread=[[NSThread alloc]initWithTarget:ce selector:@selector(CallbackThread:) object:cbtdata];
	[thread start];
	[thread release];
	[cbtdata release];
	 */
}



void UtilcreateTimer(int interval,HPData *func){
	if (timerutil==nil) {
		timerutil=[[NSUtilLib alloc]init];
	}
	[timerutil StartTimer:interval withFunc:func];
	
}
void UtildestroyTimer(){
	if (timerutil!=nil) {
		[timerutil CancelTimer];
	}
}





HPData* UtilGetStrLen(const char* str, const char* encode){
    if (!str) {
        return NULL;
    }
    
    NSString* enc = nil;
    if (!encode || strlen(encode) == 0) {
        enc = @"UTF-8";
    }else{
        enc = [NSString stringWithUTF8String:encode];
    }
    
    enc = [enc uppercaseString];
    
    NSData* data = nil;
    NSString* src = [NSString stringWithUTF8String:str];
    if ([enc isEqualToString:@"UTF-8"]) {
        data = [src dataUsingEncoding:NSUTF8StringEncoding];
    }else if ([enc isEqualToString:@"GBK"]
              || [enc isEqualToString:@"GB2312"]){
        NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
        data = [src dataUsingEncoding:enc];
    }else if ([enc isEqualToString:@"ASCII"]){
        data = [src dataUsingEncoding:NSASCIIStringEncoding];
    }else if ([enc isEqualToString:@"UNICODE"]){
        data = [src dataUsingEncoding:NSUnicodeStringEncoding];
    }
    
    int length = 0;
    if (data) {
        length = [data length];
    }
    
    HPData* retData = CreateNUMHPData(length);
    return retData;
}

//os
HPData * HpgetRoot(){
	NSString * roots=[Directory getSaveRoot];
	if (roots!=nil) {
		HPData * roodata=CreateTABLEHPData();
		HPData ** datas=(HPData **)malloc(sizeof(HPData *));
		datas[0]= CreateSHPData([roots UTF8String]);
		TableSetArray(roodata,datas,1);
		return roodata;
	}
	return NULL;
}
//os over

char * SecMd5(const char * rawdata){
	/*if (rawdata!=NULL) {
		ClientEngine * ce=[ClientEngine getClientEngine];
		NSString * rawns=[NSString stringWithUTF8String:rawdata];
		NSString * endata=[ce.securmanager MD5:rawns];
		const char * rtemp=[endata UTF8String];
		char * result=(char *)malloc(sizeof(char)*(strlen(rtemp)+1));
		strcpy(result,rtemp);
		return result;
	}*/
	return NULL;
}
char * SecDes(const char * rawdata,int flag,const char * key){
	NSString * data=[NSString stringWithUTF8String:rawdata];
	NSString * keys=nil;
	if (key!=NULL) {
		keys=[NSString stringWithUTF8String:key];
	}
	HPClientEngine * ce=[HPClientEngine getHPClientEngine];
	NSString * desstr=[ce.securmanager Des:data withKey:keys withflag:flag];
	const char * rtemp=[desstr UTF8String];
	char * result=(char *)malloc(sizeof(char)*(strlen(rtemp)+1));
	strcpy(result,rtemp);
	return result;
}
char * SecMac(const char * rawdata,const char * key){
	/*NSString * data=[NSString stringWithUTF8String:rawdata];
	NSString * keys=nil;
	if (key!=NULL) {
		keys=[NSString stringWithUTF8String:key];
	}
	ClientEngine * ce=[ClientEngine getClientEngine];
	NSString * macstr=[ce.securmanager MAC:data withKey:keys];
	const char * rtemp=[macstr UTF8String];
	char * result=(char *)malloc(sizeof(char)*(strlen(rtemp)+1));
	strcpy(result,rtemp);
	return result;*/
	return NULL;
}

int getStringLen(const char * s ,int len){
	NSString * str=[NSString stringWithUTF8String:s];
	return str.length;
}
char * getsubString(const char * s,int start,int end){
	NSString * str=[NSString stringWithUTF8String:s];
	char * returndata=NULL;
	NSRange rang;
	int i;
	rang.location=start-1;
	rang.length=end-start+1;
	if (rang.length>([str length]-rang.location)) {
		rang.length=[str length]-rang.location;
	}
	NSString * rstr=[str substringWithRange:rang];
	const char * ddss=[rstr UTF8String];
	if (ddss!=NULL) {
		int ddsslen=strlen(ddss);
		returndata=(char *)malloc(sizeof(char)*(ddsslen+1));
		for (i=0; i<ddsslen; i++) {
			returndata[i]=ddss[i];
		}
		returndata[ddsslen]='\0';
	}
	return returndata;
}

void findString(const char * s,const char * fs,int initpos,int * startpos,int * endpos){
	* startpos=-1;
	* endpos=-1;
	NSString * ss=[NSString stringWithUTF8String:s];
	NSString * fss=[NSString stringWithUTF8String:fs];
	if (([ss length]-1)<initpos || initpos<0) {
		return;
	}
	
	NSRange r;
	r.location=initpos;
	r.length=[ss length]-initpos;
	NSRange rang=[ss rangeOfString:fss options:NSCaseInsensitiveSearch range:r];
	if (rang.location!=NSNotFound) {
		*startpos=rang.location+1;
		*endpos=rang.location+rang.length;
	}
}

@implementation NSUtilLib
@synthesize utiltimer;
@synthesize timerfunc;
@synthesize interval;

-(void)TimerUpdate{	
	/*if (self.timerfunc!=NULL) {
		HPFunctionData * funcdata=[[HPFunctionData alloc] initwithFunc:[self.timerfunc getCopy]];
		ClientEngine * ce=[ClientEngine getClientEngine];
		NSThread *threadS=[[NSThread alloc]initWithTarget:ce selector:@selector(Dofunction:) object:funcdata];
		[threadS start];
		[threadS release];
		[funcdata release];
	}*/

}

-(void)doTimerUI{
	self.utiltimer=	[NSTimer scheduledTimerWithTimeInterval:self.interval/1000.0f target:self selector:@selector(TimerUpdate) userInfo:nil repeats:YES];
}

-(void)StartTimer:(int)intervalt withFunc:(HPData *)Func{
	/*[self CancelTimer];
	self.interval=intervalt;
	self.timerfunc=[[HPDataObject alloc]init];
	self.timerfunc.func=Func;
	[self performSelectorOnMainThread:@selector(doTimerUI) withObject:nil waitUntilDone:NO];*/
}
-(void)CancelTimer{
	if (self.utiltimer!=nil) {
		[self.utiltimer invalidate];
	}
	self.utiltimer=nil;
	self.timerfunc=NULL;
}
@end
